/*
 * Copyright (C) 2004, Velocityme Partnership
 * ActionActions.java
 *
 * Created on 06 October 2004, 08:25
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.ActionValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import javax.swing.tree.TreeModel;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class ActionActions extends TaskActions {
    
    /** Creates a new instance of ActionActions */
    public ActionActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ActionPK actionPK = new ActionPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("interestedContactableValues", cs.getTaskInterestedContactableValues(key, taskPK));
        session.setAttribute("responsibleContactableValues", cs.getTaskResponsibleContactableValues(key, taskPK));
        session.setAttribute("actionValue", cs.getActionValue(key, actionPK));
        session.setAttribute("actionHistory", cs.getHistoryValue(key, nodePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ActionPK actionPK = new ActionPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        session.setAttribute("taskTypePairs", NodeUtil.getAllTaskTypePairs(key, cs));
        ActionValueActionForm actionValueActionForm = new ActionValueActionForm();
        actionValueActionForm.setOwnerId(key.getPersonPK().getPersonId());
        actionValueActionForm.setPercentageComplete(new Byte((byte)0));
        actionValueActionForm.setIsInterested(Boolean.TRUE);
        actionValueActionForm.setDurationTotalMins(new Integer(0));
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("actionActionForm", actionValueActionForm);
        ActionValue actionValue = new ActionValue();
        actionValue.setTaskValue(new TaskValue());
        actionValue.getTaskValue().setNodeValue(new NodeValue());
        session.setAttribute("actionValue", actionValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskPK taskPK = new TaskPK(nodePK.getNodeId());
        ActionPK actionPK = new ActionPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        ActionValue actionValue = cs.getActionValue(key, actionPK);
        session.setAttribute("actionValue", actionValue);
        ActionValueActionForm actionValueActionForm = new ActionValueActionForm();
        actionValueActionForm.setName(actionValue.getTaskValue().getNodeValue().getName());
        actionValueActionForm.setDescription(actionValue.getTaskValue().getNodeValue().getDescription());
        actionValueActionForm.setPriority(actionValue.getTaskValue().getPriority());
        actionValueActionForm.setOwnerId(actionValue.getTaskValue().getOwnerContactableValue().getContactableId());
        actionValueActionForm.setTaskTypeId(actionValue.getTaskValue().getTaskStateMachineValue().getTaskStateMachineId());
        actionValueActionForm.setPercentageComplete(actionValue.getTaskValue().getPercentageComplete());
        actionValueActionForm.setDurationTotalMins(actionValue.getTaskValue().getExpectedDuration_min());
        session.setAttribute("days", NodeUtil.getInstance().getDurationDays());
        session.setAttribute("hours", NodeUtil.getInstance().getDurationHours());
        session.setAttribute("minutes", NodeUtil.getInstance().getMinutes());
        session.setAttribute("actionActionForm", actionValueActionForm);
        Collection priorityIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=10)
            priorityIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("priorityPairs", priorityIntegerStrings);
        Collection completeIntegerStrings = new ArrayList();
        for (int i = 0; i <= 100; i+=5)
            completeIntegerStrings.add(new IntegerString(new Integer(i), Integer.toString(i)));
        session.setAttribute("completePairs", completeIntegerStrings);
        Collection ownerIntegerStrings = new ArrayList();
        TreeModel contactableTreeModel = cs.getContactableValueTree(key);
        NodeUtil.recursiveAddContactable(contactableTreeModel, contactableTreeModel.getRoot(), ownerIntegerStrings, 0);
        session.setAttribute("ownerPairs", ownerIntegerStrings);
        Collection taskTypeIntegerStrings = new ArrayList();
        Iterator i = cs.getAllTaskStateMachineValues(key).iterator();
        while(i.hasNext()) {
            TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)i.next();
            taskTypeIntegerStrings.add(new IntegerString(taskStateMachineValue.getTaskStateMachineId(), taskStateMachineValue.toString()));
        }
        session.setAttribute("taskTypePairs", taskTypeIntegerStrings);
        session.setAttribute("allContactablePairs", NodeUtil.getAllContactablePairs(key, cs));
        Collection interestedContactableIntegerStrings = new ArrayList();
        i = cs.getTaskInterestedContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            interestedContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("interestedContactablePairs", interestedContactableIntegerStrings);
        Collection responsibleContactableIntegerStrings = new ArrayList();
        i = cs.getTaskResponsibleContactableValues(key, taskPK).iterator();
        while(i.hasNext()) {
            ContactableValue contactableValue = (ContactableValue)i.next();
            responsibleContactableIntegerStrings.add(new IntegerString(contactableValue.getContactableId(), ContactableValueToString.toString(contactableValue)));
        }
        session.setAttribute("responsibleContactablePairs", responsibleContactableIntegerStrings);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            ActionValueActionForm actionValueActionForm = (ActionValueActionForm)form;
            ActionValue actionValue = (ActionValue)session.getAttribute("actionValue");
            actionValue.getTaskValue().getNodeValue().setName(actionValueActionForm.getName());
            actionValue.getTaskValue().getNodeValue().setDescription(actionValueActionForm.getDescription());
            actionValue.getTaskValue().setPriority(actionValueActionForm.getPriority());
            ContactableValue ownerContactableValue = new ContactableValue();
            ownerContactableValue.setPrimaryKey(new ContactablePK(actionValueActionForm.getOwnerId()));
            actionValue.getTaskValue().setOwnerContactableValue(ownerContactableValue);
            TaskStateMachineValue taskStateMachineValue = new TaskStateMachineValue();
            taskStateMachineValue.setPrimaryKey(new TaskStateMachinePK(actionValueActionForm.getTaskTypeId()));
            actionValue.getTaskValue().setTaskStateMachineValue(taskStateMachineValue);
            actionValue.getTaskValue().setPercentageComplete(actionValueActionForm.getPercentageComplete());
            actionValue.getTaskValue().setExpectedDuration_min(actionValueActionForm.getDurationTotalMins());
            ActionPK actionPK;
            if (actionValue.actionIdHasBeenSet()) {
                actionPK = actionValue.getPrimaryKey();
                cs.actionEdit(key, actionPK, actionValue, actionValueActionForm.getResponsibleContactablePKs(), actionValueActionForm.getInterestedContactablePKs());
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                actionPK = cs.actionCreate(key, parentNodePK, actionValue);
                if (((ActionValueActionForm)form).getIsInterested().equals(Boolean.TRUE))
                    cs.taskEnableInterestedContactableStateValue(key, new TaskPK(actionPK.getActionId()));
            }
            nodePK = new NodePK(actionPK.getActionId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
